<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

class CreateOrderReturnsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasTable('order_returns')) {
            Schema::create('order_returns', function (Blueprint $table) {
                $table->id();
                $table->integer('order_id')->comment('订单id');
                $table->enum('type', ['refund', 'return_goods', 'exchange_goods'])->comment('售后类型:refund退款,return_goods退货,exchange_goods换货');
                $table->tinyInteger('status')->comment('售后状态:0待审核,1审核通过,[2待退货,]3已退货,[4退款中,]5已退款,[6待换货,]7已换货,8拒绝,9取消');
                $table->text('reason')->comment('售后原因');
                $table->string('voucher_images')->comment('凭证图片');
                $table->json('product_json')->comment("商品信息,数据示例:[{'product_id':'商品id','spec_id':'商品规格id','num':'退货数量','amount':'退款金额'}]");
                $table->string('express_num')->nullable()->comment('快递单号');
                $table->json('exchange_product_json')->nullable()->comment("换货商品信息,数据示例:[{'product_id':'商品id','spec_id':'商品规格id','num':'数量'}]");
                $table->string('exchange_express_num')->nullable()->comment('换货发货快递单号');
                $table->integer('refund_amount')->default(0)->comment('退款总金额');
                $table->integer('refund_time')->nullable()->comment('退款时间');
                $table->text('refuse_reason')->nullable()->comment('拒绝原因');
                $table->softDeletes();
                $table->timestamps();
                $table->index(['order_id']);
            });
            DB::statement("ALTER TABLE `order_returns` comment '订单售后表'");
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('order_returns');
    }
}
